<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Module: ExportToSpreadsheet</title>
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />

<script type="text/javascript" charset="utf-8">
  relpath = '';
  if (relpath != '') relpath += '/';
</script>
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>

  </head>
  <body>
    <script type="text/javascript" charset="utf-8">
      if (window.top.frames.main) document.body.className = 'frames';
    </script>
    
    <div id="header">
      <div id="menu">
  
    <a href="_index.html">Index (E)</a> &raquo; 
    
    
    <span class="title">ExportToSpreadsheet</span>
  
  
  <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
</div>

      <div id="search">
  <a id="class_list_link" href="#">Class List</a>
  <a id="method_list_link" href="#">Method List</a>
  <a id ="file_list_link" href="#">File List</a>
</div>

      <div class="clear"></div>
    </div>
    
    <iframe id="search_frame"></iframe>
    
    <div id="content"><h1>Module: ExportToSpreadsheet
  
  
  
</h1>

<dl class="box">
  
  
    
  
    
  
  
  
    <dt class="r1 last">Defined in:</dt>
    <dd class="r1 last">lib/export_to_spreadsheet.rb</dd>
  
</dl>
<div class="clear"></div>

<h2>Overview</h2><div class="docstring">
  <div class="discussion">
    <p>
This is the module to include into an ActiveRecord model in order to make
its data exportable. You will have to also define a compose_export method
that tells the plug-in what data to export.
</p>


  </div>
</div>
<div class="tags">
  
  <div class="examples">
    <h3>Examples:</h3>
    
      <h4><div class='inline'></div></h4>
      <pre class="example code"><span class='class class kw'>class</span> <span class='Foo constant id'>Foo</span> <span class='lt op'>&lt;</span> <span class='ActiveRecord constant id'>ActiveRecord</span><span class='colon2 op'>::</span><span class='Base constant id'>Base</span>
  <span class='include identifier id'>include</span> <span class='ExportToSpreadsheet constant id'>ExportToSpreadsheet</span>

  <span class='def def kw'>def</span> <span class='compose_export identifier id'>compose_export</span>
    <span class='@doc ivar id'>@doc</span><span class='dot token'>.</span><span class='write identifier id'>write</span> <span class='do do kw'>do</span> <span class='bitor op'>|</span><span class='d identifier id'>d</span><span class='bitor op'>|</span>
      <span class='d identifier id'>d</span><span class='dot token'>.</span><span class='title_1 identifier id'>title_1</span><span class='lparen token'>(</span><span class='string val'>'Export of foo'</span><span class='rparen token'>)</span>
      <span class='d identifier id'>d</span><span class='dot token'>.</span><span class='line identifier id'>line</span><span class='lparen token'>(</span><span class='dstring node'>&quot;My foo is #{self.name}&quot;</span><span class='rparen token'>)</span>
    <span class='end end kw'>end</span>
  <span class='end end kw'>end</span>
</pre>
    
  </div>

</div>
  <h2>Constant Summary</h2>
  
    <dl class="constants">
      
        <dt id="export_use_fork-classvariable" class="">@@export_use_fork =
          <div class="docstring">
  <div class="discussion">
    <p>
Whether we want to use fork to run the export or not
</p>


  </div>
</div>
<div class="tags">
  
</div>
        </dt>
        <dd><pre class="code"><span class='true true kw'>true</span>
</pre></dd>
      
    </dl>
  



  
    <h2>
      Class Method Summary
      <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
    </h2>

    <ul class="summary">
      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#export_use_fork%3D-class_method" title="export_use_fork= (class method)">+ (Object) <strong>export_use_fork=</strong>(val) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'></div></span>
  
</li>

      
    </ul>
  
    <h2>
      Instance Method Summary
      <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
    </h2>

    <ul class="summary">
      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#make_token-instance_method" title="#make_token (instance method)">- (Object) <strong>make_token</strong> </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>
Generates a complex String to ensure that a filename is uniq.
</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#to_excel-instance_method" title="#to_excel (instance method)">- (Object) <strong>to_excel</strong>(*args) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>
The actual export (starting the JVM and exporting) is done in a forked
process that can be terminated once the file is written.
</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#to_google_spreadsheets-instance_method" title="#to_google_spreadsheets (instance method)">- (Object) <strong>to_google_spreadsheets</strong>(*args) </a>
    

    
  </span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'></div></span>
  
</li>

      
    </ul>
  



  <div id="class_method_details" class="method_details_list">
    <h2>Class Method Details</h2>
    
    
      <div class="method_details first">
  <p class="signature first" id="export_use_fork=-class_method">
  
    + (<tt>Object</tt>) <strong>export_use_fork=</strong>(val) 
  

  
</p><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


23
24
25</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/export_to_spreadsheet.rb', line 23</span>

<span class='def def kw'>def</span> <span class='self self kw'>self</span><span class='dot token'>.</span><span class='export_use_fork identifier id'>export_use_fork</span><span class='assign token'>=</span> <span class='val identifier id'>val</span>
  <span class='@@export_use_fork ivar id'>@@export_use_fork</span> <span class='assign token'>=</span> <span class='val identifier id'>val</span>
<span class='end end kw'>end</span>
</pre>
    </td>
  </tr>
</table>
</div>
    
  </div>

  <div id="instance_method_details" class="method_details_list">
    <h2>Instance Method Details</h2>
    
    
      <div class="method_details first">
  <p class="signature first" id="make_token-instance_method">
  
    - (<tt>Object</tt>) <strong>make_token</strong> 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>
Generates a complex String to ensure that a filename is uniq
</p>


  </div>
</div>
<div class="tags">
  
</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


28
29
30
31
32</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/export_to_spreadsheet.rb', line 28</span>

<span class='def def kw'>def</span> <span class='make_token identifier id'>make_token</span>
  <span class='comment val'># From the restful-authentication plug-in</span>
  <span class='args identifier id'>args</span> <span class='assign token'>=</span> <span class='lbrack token'>[</span> <span class='Time constant id'>Time</span><span class='dot token'>.</span><span class='now identifier id'>now</span><span class='comma token'>,</span> <span class='lparen token'>(</span><span class='float val'>1</span><span class='dot2 op'>..</span><span class='integer val'>10</span><span class='rparen token'>)</span><span class='dot token'>.</span><span class='map identifier id'>map</span><span class='lbrace token'>{</span> <span class='rand identifier id'>rand</span><span class='dot token'>.</span><span class='to_s identifier id'>to_s</span> <span class='rbrace token'>}</span> <span class='rbrack token'>]</span>
  <span class='Digest constant id'>Digest</span><span class='colon2 op'>::</span><span class='SHA1 constant id'>SHA1</span><span class='dot token'>.</span><span class='hexdigest identifier id'>hexdigest</span><span class='lparen token'>(</span><span class='args identifier id'>args</span><span class='dot token'>.</span><span class='flatten identifier id'>flatten</span><span class='dot token'>.</span><span class='join identifier id'>join</span><span class='lparen token'>(</span><span class='string val'>'--'</span><span class='rparen token'>)</span><span class='rparen token'>)</span>
<span class='end end kw'>end</span>
</pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="to_excel-instance_method">
  
    - (<tt>Object</tt>) <strong>to_excel</strong>(*args) 
  

  
</p><div class="docstring">
  <div class="discussion">
    <p>
The actual export (starting the JVM and exporting) is done in a forked
process that can be terminated once the file is written. This is to make
sure that the (high amount of) memory used by the JVM is freed once its job
is over. With MySQL this messes a little with database connections so we
have to close the connection and reopen it in each process. On Windows,
this does not work so when do a basic export then.
</p>


  </div>
</div>
<div class="tags">
  <h3>Parameters:</h3>
<ul class="param">
  
    <li>
      
        <span class='type'>(<tt>Hash</tt>)</span>
      
      
        <span class='name'>args</span>
      
      
      
        &mdash;
        <div class='inline'><p>
hash for export configuration
</p>
</div>
      
    </li>
  
</ul>

  
    
    
<h3>Returns:</h3>
<ul class="return">
  
    <li>
      
        <span class='type'></span>
      
      
      
      
        
        <div class='inline'><p>
the document it-self, which is a class of <span class='object_link'><a href="SalesClicExporter/Document.html" title="SalesClicExporter::Document (module)">SalesClicExporter::Document</a></span>
</p>
</div>
      
    </li>
  
</ul>
<h3>Raises:</h3>
<ul class="raise">
  
    <li>
      
        <span class='type'>(<tt>RunTimeError</tt>)</span>
      
      
      
      
        &mdash;
        <div class='inline'><p>
an exception is raised when an error occurs during the export. The log has
to be checked to see what happened
</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/export_to_spreadsheet.rb', line 47</span>

<span class='def def kw'>def</span> <span class='to_excel identifier id'>to_excel</span><span class='lparen token'>(</span><span class='mult op'>*</span><span class='args identifier id'>args</span><span class='rparen token'>)</span>
  <span class='comment val'># Setting up a few variable so we can find the file once it has been written</span>
  <span class='export identifier id'>export</span> <span class='assign token'>=</span> <span class='prepare_to_excel identifier id'>prepare_to_excel</span><span class='lparen token'>(</span><span class='mult op'>*</span><span class='args identifier id'>args</span><span class='rparen token'>)</span>

  <span class='comment val'># Closing the DB connection before forking, we store the config to be able to reconnect easily</span>
  <span class='dbconfig identifier id'>dbconfig</span> <span class='assign token'>=</span> <span class='ActiveRecord constant id'>ActiveRecord</span><span class='colon2 op'>::</span><span class='Base constant id'>Base</span><span class='dot token'>.</span><span class='remove_connection identifier id'>remove_connection</span>

  <span class='comment val'># fork forks the process and returns</span>
  <span class='comment val'>#   the pid of the child process in the father process</span>
  <span class='comment val'>#   and the nil in the child process</span>
  <span class='comment val'># so we know in which process we are depending on the returned value</span>
  <span class='begin begin kw'>begin</span>
    <span class='if if kw'>if</span> <span class='@@export_use_fork ivar id'>@@export_use_fork</span>
      <span class='if if kw'>if</span> <span class='fork identifier id'>fork</span>
        <span class='comment val'># This is the father process</span>
        <span class='ActiveRecord constant id'>ActiveRecord</span><span class='colon2 op'>::</span><span class='Base constant id'>Base</span><span class='dot token'>.</span><span class='establish_connection identifier id'>establish_connection</span><span class='lparen token'>(</span><span class='dbconfig identifier id'>dbconfig</span><span class='rparen token'>)</span>
        <span class='comment val'># We just wait for the child process to have finished the export</span>
        <span class='comment val'># wait2 returns [pid, exit_code] # exit_code has the class Process::Status</span>
        <span class='exit_code identifier id'>exit_code</span> <span class='assign token'>=</span> <span class='Process constant id'>Process</span><span class='dot token'>.</span><span class='wait2 identifier id'>wait2</span><span class='dot token'>.</span><span class='last identifier id'>last</span><span class='dot token'>.</span><span class='to_i identifier id'>to_i</span>
      <span class='else else kw'>else</span>
        <span class='begin begin kw'>begin</span>
          <span class='comment val'># This is the child process</span>
          <span class='ActiveRecord constant id'>ActiveRecord</span><span class='colon2 op'>::</span><span class='Base constant id'>Base</span><span class='dot token'>.</span><span class='establish_connection identifier id'>establish_connection</span><span class='lparen token'>(</span><span class='dbconfig identifier id'>dbconfig</span><span class='rparen token'>)</span>
          <span class='comment val'># Doing the actual export</span>
          <span class='comment val'># This is what starts the JVM</span>
          <span class='compute_to_excel identifier id'>compute_to_excel</span><span class='lparen token'>(</span><span class='mult op'>*</span><span class='args identifier id'>args</span><span class='rparen token'>)</span>
          <span class='exit! fid id'>exit!</span><span class='lparen token'>(</span><span class='integer val'>0</span><span class='rparen token'>)</span> <span class='comment val'># Terminating the process</span>
        <span class='rescue rescue kw'>rescue</span> <span class='Exception constant id'>Exception</span> <span class='assign token'>=</span><span class='gt op'>&gt;</span> <span class='e identifier id'>e</span> <span class='comment val'># need to make sure the child process finishes even if the export fails</span>
          <span class='logger identifier id'>logger</span><span class='dot token'>.</span><span class='error identifier id'>error</span> <span class='e identifier id'>e</span><span class='dot token'>.</span><span class='message identifier id'>message</span>
          <span class='logger identifier id'>logger</span><span class='dot token'>.</span><span class='error identifier id'>error</span> <span class='e identifier id'>e</span><span class='dot token'>.</span><span class='backtrace identifier id'>backtrace</span><span class='dot token'>.</span><span class='join identifier id'>join</span><span class='lparen token'>(</span><span class='string val'>&quot;\n&quot;</span><span class='rparen token'>)</span>
          <span class='exit! fid id'>exit!</span><span class='lparen token'>(</span><span class='integer val'>1</span><span class='rparen token'>)</span>
        <span class='end end kw'>end</span>
      <span class='end end kw'>end</span>
    <span class='else else kw'>else</span>
      <span class='raise identifier id'>raise</span> <span class='string val'>&quot;not even trying&quot;</span>
    <span class='end end kw'>end</span>

  <span class='comment val'># Handles at least the Windows case where the fork function is not implemented.</span>
  <span class='comment val'># In this case do a simple export without forking the process</span>
  <span class='rescue rescue kw'>rescue</span> <span class='Exception constant id'>Exception</span> <span class='assign token'>=</span><span class='gt op'>&gt;</span> <span class='e identifier id'>e</span>
    <span class='ActiveRecord constant id'>ActiveRecord</span><span class='colon2 op'>::</span><span class='Base constant id'>Base</span><span class='dot token'>.</span><span class='establish_connection identifier id'>establish_connection</span><span class='lparen token'>(</span><span class='dbconfig identifier id'>dbconfig</span><span class='rparen token'>)</span>
    <span class='compute_to_excel identifier id'>compute_to_excel</span><span class='lparen token'>(</span><span class='mult op'>*</span><span class='args identifier id'>args</span><span class='rparen token'>)</span>
    <span class='exit_code identifier id'>exit_code</span> <span class='assign token'>=</span> <span class='integer val'>0</span>
  <span class='end end kw'>end</span>

  <span class='comment val'># If the export did not finish normally, we raise an error</span>
  <span class='raise identifier id'>raise</span> <span class='string val'>&quot;Unexpected error during export&quot;</span> <span class='if if_mod kw'>if</span> <span class='exit_code identifier id'>exit_code</span> <span class='neq op'>!=</span> <span class='integer val'>0</span>

  <span class='comment val'># It worked, we return the export</span>
  <span class='return return kw'>return</span> <span class='export identifier id'>export</span>
<span class='end end kw'>end</span>
</pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <p class="signature " id="to_google_spreadsheets-instance_method">
  
    
      <span class="overload">- (<tt>Object</tt>) <strong>to_google_spreadsheets</strong>(oauth_token) </span>
    
      <span class="overload">- (<tt>Object</tt>) <strong>to_google_spreadsheets</strong>(google_login, google_password) </span>
    
  

  
</p><div class="docstring">
  <div class="discussion">
    

  </div>
</div>
<div class="tags">
  
  <h3>Overloads:</h3>
  <ul class="overload">
    
      
      <li class="overload_item">
        <span class="signature">- (<tt>Object</tt>) <strong>to_google_spreadsheets</strong>(oauth_token) </span>
        <div class="docstring">
  <div class="discussion">
    <p>
Exports a model to a Google Spreadsheet doc using a OAuth token
</p>


  </div>
</div>
<div class="tags">
  
</div>
      </li>
    
      
      <li class="overload_item">
        <span class="signature">- (<tt>Object</tt>) <strong>to_google_spreadsheets</strong>(google_login, google_password) </span>
        <div class="docstring">
  <div class="discussion">
    <p>
Exports a model to a Google Spreadsheet doc using a Google account
credentials
</p>


  </div>
</div>
<div class="tags">
  
</div>
      </li>
    
  </ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


103
104
105
106</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/export_to_spreadsheet.rb', line 103</span>

<span class='def def kw'>def</span> <span class='to_google_spreadsheets identifier id'>to_google_spreadsheets</span><span class='lparen token'>(</span><span class='mult op'>*</span><span class='args identifier id'>args</span><span class='rparen token'>)</span>
  <span class='prepare_export identifier id'>prepare_export</span><span class='lparen token'>(</span><span class='symbol val'>:google_spreadsheets</span><span class='comma token'>,</span> <span class='mult op'>*</span><span class='args identifier id'>args</span><span class='rparen token'>)</span>
  <span class='return return kw'>return</span> <span class='compute_export identifier id'>compute_export</span><span class='lparen token'>(</span><span class='symbol val'>:google_spreadsheets</span><span class='comma token'>,</span> <span class='mult op'>*</span><span class='args identifier id'>args</span><span class='rparen token'>)</span>
<span class='end end kw'>end</span>
</pre>
    </td>
  </tr>
</table>
</div>
    
  </div>

</div>
    
    <div id="footer">
  Generated on 05/16/11 10:59:52 by 
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
  0.6.7 (ruby-1.8.7).
</div>

  </body>
</html>